package com.daqsoft.controller.project;

import com.daqsoft.data.project.Node;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.dto.StatusEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.project.NodeService;
import com.daqsoft.service.project.ProjectService;
import com.daqsoft.service.project.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周麟东 on 18:16
 */
@RestController
@Verify(desc = "流程节点管理")
@RequestMapping("node")
public class NodeController {

    @Autowired
    private NodeService nodeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProjectService projectService;

    @RequestMapping("save")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "保存节点")
    public GeneralResultEnum saveNode(int projectId , Node node){
        Project project = projectService.get(projectId);
        if (project == null){
            return GeneralResultEnum.FAIL.setDesc("项目不存在 id = "+projectId);
        }
        return GeneralResultEnum.SUCCESS.setData(nodeService.save(project,node).toMap()).setDesc("保存成功");
    }

    @RequestMapping("getNodesByProject")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "通过项目ID获取所有节点ID")
    public GeneralResultEnum getNodesByProject(Integer projectId){
        if (projectId == null){
            return GeneralResultEnum.SUCCESS;
        }
        List<Map<String,Object>> nodes = new ArrayList<>();
        for (Node node : nodeService.getNodesByProject(projectId)) {
            nodes.add(node.toMap());
        }
        return GeneralResultEnum.SUCCESS.setData(nodes).setChildren(nodes).setDesc("获取节点成功");
    }
    @RequestMapping("getNodesOrTasks")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取子任务")
    public GeneralResultEnum getNodesOrTasks(Integer projectId , String node){
        if(node.equals("root") && projectId == null){
            return GeneralResultEnum.FAIL.setDesc("projectId不存在");
        }
        if (node.equals("root")){
            return getNodesByProject(projectId);
        }
        //如果不是root则返回task
        List<Map<String,Object>> tasks = new ArrayList<>();
        Integer nodeId = Integer.parseInt(node);
        List<Task> taskList;
        if (nodeId > 100000){//如果是大于100000的则是任务节点
            taskList = taskService.getTaskByPid(nodeId);
        }else {
            taskList = taskService.getTaskByNode(nodeId);
        }
        for (Task task : taskList) {
            tasks.add(task.toMap());
        }
        return GeneralResultEnum.SUCCESS.setData(tasks).setDesc("获取任务成功");
    }

//    /**
//     * 获取上一个节点
//     * @param projectId 项目ID
//     * @param nodeId 当前节点的id
//     * @return
//     */
//    @RequestMapping("getHaveNoNextNode")
//    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取没有下个节点的节点列表")
//    public GeneralResultEnum getHaveNoNextNode(int projectId , Integer nodeId){
//        Project project = projectService.get(projectId);
//        if (project == null){
//            return GeneralResultEnum.FAIL.setDesc("项目不存在 id = "+projectId);
//        }
//        if (nodeId == null || nodeId < 1){
//
//        }
//        return GeneralResultEnum.SUCCESS.setData(nodes).setDesc("获取节点成功");
//    }

    @RequestMapping("remove")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "删除节点")
    public GeneralResultEnum remove(int id){
        Node node = nodeService.get(id);
        if (node == null){
            return GeneralResultEnum.FAIL.setDesc("该节点已经不存在");
        }
        if (node.getTasks() != null && node.getTasks().size() > 0){
            return GeneralResultEnum.FAIL.setDesc("必须先删除该节点下的所有任务");
        }
        nodeService.remove(node);
        return GeneralResultEnum.SUCCESS.setDesc("删除节点成功");
    }

    /**
     * 拷贝节点
     * @param fromId 从哪个项目拷贝
     * @param targetId 拷贝到哪个项目
     * @return 结果
     */
    @RequestMapping("copyNode")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "拷贝节点")
    public GeneralResultEnum copyNode(int fromId , int targetId){

        Project from = projectService.get(fromId);
        if (from == null){
           return GeneralResultEnum.FAIL.setDesc("被拷贝的项目为空");
        }
        Project target = projectService.get(targetId);
        if (target == null){
            return GeneralResultEnum.FAIL.setDesc("拷贝到的项目为空");
        }
        nodeService.copyNodes(from,target);
        return GeneralResultEnum.SUCCESS;
    }
    @RequestMapping("updateChangeNode")
    @Verify(value = VerifyType.HAVE_PERMISSION , desc = "更新正在变更的节点")
    public GeneralResultEnum updateChangeNode(int id , String status){
        Node node = nodeService.get(id);
        if (node == null){
            return GeneralResultEnum.FAIL.setDesc("节点不存在 id = "+id);
        }
        node = nodeService.updateChangeNode(node , StatusEnum.valueOf(status));
        return GeneralResultEnum.SUCCESS.setData(node.toMap()).setDesc("保存节点成功");
    }
    @RequestMapping("down")
    @Verify(value = VerifyType.HAVE_PERMISSION , desc = "将节点向下移动")
    public GeneralResultEnum down(int id){
        Node node = nodeService.get(id);
        if (node == null){
            return GeneralResultEnum.FAIL.setDesc("节点不存在 id = "+id);
        }
        node = nodeService.down(node);
        if (node  == null){
            return GeneralResultEnum.FAIL.setDesc("不能向下移动节点 , 因为该节点已经处于末尾了");
        }
        return GeneralResultEnum.SUCCESS;
    }
    @RequestMapping("up")
    @Verify(value = VerifyType.HAVE_PERMISSION , desc = "将节点向下移动")
    public GeneralResultEnum up(int id){
        Node node = nodeService.get(id);
        if (node == null){
            return GeneralResultEnum.FAIL.setDesc("节点不存在 id = "+id);
        }
        node = nodeService.up(node);
        if (node  == null){
            return GeneralResultEnum.FAIL.setDesc("不能向上移动节点 , 因为该节点已经处于首位了");
        }
        return GeneralResultEnum.SUCCESS;
    }
}
